package com.ktkq.action.course;



import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.ktkq.action.BaseAction;
import com.ktkq.service.course.CourseService;
import com.ktkq.util.PageFinder;
import com.ktkq.util.Pageing;
import com.ktkq.util.StringUtil;
import com.ktkq.util.TimeUtil;
import com.ktkq.vo.Academy;
import com.ktkq.vo.ClassRoom;
import com.ktkq.vo.Course;


@SuppressWarnings("serial")
@Controller
public class CourseAction extends BaseAction {
	
	private Pageing pageing;
	
	private PageFinder<Course> pageFinder;
	
	 private List<File> file;
	 private List<String> fileFileName;
	 private List<String> fileContentType;
  
	
	 @Resource(name="courseService") 
	 CourseService courseService;
	
	 Course course;
	
	int courseId;

	  int[] deleteList;  //批量删除
	String resultView;    //结果视图       	    
	@Override
	public String execute() throws Exception {
		if(pageFinder==null){
			pageFinder=new PageFinder<Course>();
		}

		
		return INPUT;
	}
	/**
	 * 查询分页信息
	 * @return
	 */
	public String findCourseList(){
		if(pageing==null){
			pageing=new Pageing();
		}
		if(pageFinder==null){
			pageFinder=new PageFinder<Course>();
		}
		int page=pageFinder.getPageNo();
		pageFinder.setPageSize(20);
		pageing.setPageSize(20);
		pageing.setStartOfPage(pageFinder.getStartOfPage());
		//分页信息
		pageFinder=courseService.queryList(pageing);
		pageFinder.setPageNo(page);
		pageFinder.setPageSize(20);
		pageFinder.refresh();
		return SUCCESS;
	}
	/**
	 * 通过ID查询信息
	 * @return
	 */
	public String findCourseById(){
		if(pageFinder==null){
			pageFinder=new PageFinder<Course>();
		}
		
			pageFinder.setDataList(courseService.queryListById(courseId));

			return "edit";
	}
	/**
	 * 添加信息
	 * @return
	 */
	public String addCourse(){
		if(course !=null){
			course.setTime(new TimeUtil().getTime());
			courseService.insert(course);
		}
		if(pageFinder==null){
			pageFinder=new PageFinder<Course>();
		}
		List<Course> list = new ArrayList<Course>();
		list.add(course);
		pageFinder.setDataList(list);
		return INPUT;
	}
	
	/**
	 * 修改信息
	 * @return
	 */
	public String updateCourse(){
		if(course !=null){
			courseService.update(course);
		
		}
		resultView = "/admin/course_findCourseList.action?pageFinder.pageNo=1";
		return "update";
	}
	/**
	 * 删除信息
	 * @return
	 */
	public String deleteCourse(){
		if(deleteList!=null){
		      for(int id:deleteList){
		    	  Course stu =  new Course();
		    	  stu.setId(id);
		    	  courseService.delete(stu);
		      }
			}else if(course !=null){
			courseService.delete(course);
		}
		resultView = "/admin/course_findCourseList.action?pageFinder.pageNo=1";
		return "update";
	}
	
	
	/**
	 *获取课程 
	 */
	public void queryCourse(){
		if(pageing==null)
			pageing=new Pageing();
		
			pageFinder=new PageFinder<Course>();
		
			pageFinder.setPageSize(20);
		pageing.setPageSize(20);
		pageing.setStartOfPage(pageFinder.getStartOfPage());
		pageFinder = courseService.queryList(pageing);
		
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			Gson g  = new Gson();
	        String json = g.toJson(new Gson().toJson(pageFinder));
	        PrintWriter pw = response.getWriter();
	        pw.print(json);
	        pw.flush();
	        pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	public Pageing getPageing() {
		return pageing;
	}
	public void setPageing(Pageing pageing) {
		this.pageing = pageing;
	}
	
	
	 public PageFinder<Course> getPageFinder() {
		return pageFinder;
	}
	public void setPageFinder(PageFinder<Course> pageFinder) {
		this.pageFinder = pageFinder;
	}
	public CourseService getCourseService() {
		return courseService;
	}
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}
	public Course getCourse() {
		return course;
	}
	public void setCourse(Course course) {
		this.course = course;
	}
	public int getCourseId() {
		return courseId;
	}
	public void setCourseId(int courseId) {
		this.courseId = courseId;
	}
	/**
     * 返回上传文件的保存位置
     * 
     * @return
     */
    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath("/studentFile"); 
    }

 
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	public int[] getDeleteList() {
		return deleteList;
	}
	public void setDeleteList(int[] deleteList) {
		this.deleteList = deleteList;
	}
	public String getResultView() {
		return resultView;
	}
	public void setResultView(String resultView) {
		this.resultView = resultView;
	}

	

	

  
	

}
